package com.ziyueyuwen.xiniao.controller.product;

import com.suyeer.basic.util.HttpResUtil;
import com.ziyueyuwen.xiniao.base.HttpResContent;
import com.ziyueyuwen.xiniao.bean.page.PageData;
import com.ziyueyuwen.xiniao.bean.product.ProductListQuery;
import com.ziyueyuwen.xiniao.bean.product.ProductListVO;
import com.ziyueyuwen.xiniao.bean.product.ProductSaveAO;
import com.ziyueyuwen.xiniao.bean.ZyProductInfo;
import com.ziyueyuwen.xiniao.bean.select.SelectBean;
import com.ziyueyuwen.xiniao.bean.vo.ZyCourseVO;
import com.ziyueyuwen.xiniao.enums.DataActionEnum;
import com.ziyueyuwen.xiniao.enums.ParamTypeEnum;
import com.ziyueyuwen.xiniao.service.ProductService;
import com.ziyueyuwen.xiniao.valid.group.interfaces.product.ProductSave;
import com.ziyueyuwen.xiniao.valid.group.interfaces.product.ProductUpdate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.List;

import static com.ziyueyuwen.xiniao.base.BaseUtil.checkParam;
import static com.ziyueyuwen.xiniao.base.BaseUtil.saveErrorMsgAndData;
import static com.ziyueyuwen.xiniao.base.BaseUtil.saveSuccessMsgAndData;
import static com.ziyueyuwen.xiniao.enums.ParamTypeEnum.INTEGER_GT_0;
import static com.ziyueyuwen.xiniao.util.ConstUtil.*;

/**
 * @author wancheng  on 2019/2/21.
 */
@Controller
@RequestMapping("/product")
@Validated
public class ProductController {

    @Autowired
    ProductService productService;

    @RequestMapping(value = "/save_product.do", method = RequestMethod.POST)
    public void saveProduct(HttpServletRequest request,
                            HttpServletResponse response,
                            @Validated({ProductSave.class}) ProductSaveAO product) {
        HttpResContent<Boolean> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("新增商品");
        try {
            productService.saveSelective(product);

            saveSuccessMsgAndData(hrc, true);
        } catch (Exception e) {
            e.printStackTrace();
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/update_product.do", method = RequestMethod.POST)
    public void updateProduct(HttpServletRequest request,
                              HttpServletResponse response,
                              @Validated({ProductUpdate.class}) ProductSaveAO product) {
        HttpResContent<Boolean> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("修改商品");
        try {
            productService.updateSelective(product);
            saveSuccessMsgAndData(hrc, true);
        } catch (Exception e) {
            e.printStackTrace();
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/get_product_by_id.do", method = RequestMethod.POST)
    public void getProductById(HttpServletRequest request,
                               HttpServletResponse response,
                               @RequestParam(PARAM_PRODUCT_ID) Integer productId) {
        HttpResContent<ZyProductInfo> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("查询商品");
        try {
            checkParam(INTEGER_GT_0, PARAM_PRODUCT_ID, productId);
            saveSuccessMsgAndData(hrc, productService.getProductAndStageByPreKey(productId));
        } catch (Exception e) {
            e.printStackTrace();
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/get_course.do", method = RequestMethod.POST)
    public void getCourse(HttpServletRequest request,
                          HttpServletResponse response,
                          @RequestParam(value = PARAM_COURSE_NAME, required = false) String courseName) {
        HttpResContent<List<ZyCourseVO>> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("查询课程");
        try {
            saveSuccessMsgAndData(hrc, productService.getCourseInfo(courseName));
        } catch (Exception e) {
            e.printStackTrace();
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/get_class.do", method = RequestMethod.POST)
    public void getClass(HttpServletRequest request,
                         HttpServletResponse response,
                         @RequestParam(value = PARAM_COURSE_ID, required = true) Integer courseId,
                         @RequestParam(value = PARAM_CLASS_NAME, required = false) String className,
                         Integer productId,
                         DataActionEnum action) {
        HttpResContent<List<SelectBean>> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("查询班级");
        try {
            checkParam(ParamTypeEnum.INTEGER_GT_0, PARAM_COURSE_ID, courseId);
            saveSuccessMsgAndData(hrc, productService.getClassInfo(courseId, className, action, productId));
        } catch (Exception e) {
            e.printStackTrace();
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/upper_or_lower_shelf.do", method = RequestMethod.POST)
    public void upperOrLowerShelf(HttpServletRequest request,
                                  HttpServletResponse response,
                                  @RequestParam(value = PARAM_PRODUCT_ID) Integer productId,
                                  @RequestParam(value = PARAM_PRODUCT_STATUS) Boolean productStatus) {
        HttpResContent<Boolean> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("商品的上下架");
        try {
            checkParam(ParamTypeEnum.INTEGER_GT_0, PARAM_PRODUCT_ID, productId);
            productService.productUpperOrLowerShelf(productId, productStatus);
            saveSuccessMsgAndData(hrc, true);
        } catch (Exception e) {
            e.printStackTrace();
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/get_product_list.do", method = RequestMethod.POST)
    public void getProductList(HttpServletRequest request,
                               HttpServletResponse response,
                               @Validated ProductListQuery productListQuery) {
        HttpResContent<PageData<ProductListVO>> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("后台商品查询列表");
        try {
            PageData<ProductListVO> pageData = productService.getProductListForPc(productListQuery);
            saveSuccessMsgAndData(hrc, pageData);
        } catch (Exception e) {
            e.printStackTrace();
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }
}
